import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {

    public static final String[] suits = {"♥","♠","♦","♣"};//不变的值

    //买牌：有序的
    public List<Card> buyCard() {
        List<Card> listcard = new ArrayList<>();

        //每个花色买13张牌
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                String suit = suits[j];
                int rank = i;
                Card card = new Card(suit,rank);

                listcard.add(card);
            }
        }
        return listcard;
    }

    //洗牌：
    private void swap(List<Card> cards,int i,int index) {
        /*
        我们这里想的交换元素是不是想要这么交换，不可以，
        因为我们cards里面是数组，而非cards是数组
        Card c = cards[i];
        cards[i] = cards[index];
        cards[index] = c;
         */
        //我们这样交换：
        Card c = cards.get(i);
        cards.set(i,cards.get(index));
        cards.set(index,c);
    }
    public void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = cards.size() - 1; i > 0; i--) {
            int index = random.nextInt(i);
            //这里呢假如我们i是51，这里就生成0-50的随机数
            //进行交换元素
            swap(cards,i,index);
        }
    }

    //发牌：
    public List<List<Card>> play(List<Card> cards) {
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);

                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
